'''
5.2.3 修改Excel文件中单元格样式
'''

import openpyxl

# 创建工作薄对象
wb = openpyxl.Workbook()
ws = wb.active
rows = [
   ['ID', 'name', 'age', 'gender'],
   [1, '张三', 28, 'man'],
   [2, '李四', 25, 'woman'],
   [3, '王五', 40, 'man']
]
for row in rows:
   # 添加多行
   ws.append(row)

# 字体样式
from openpyxl.styles import Font, colors
font = Font(name='微软雅黑', size=25, italic=True, color=colors.RED, bold=True)
ws['A1'].font = font

# 填充样式
from openpyxl.styles import PatternFill
fill = PatternFill(fill_type = 'solid',start_color= colors.GREEN)
ws['B1'].fill = fill

# 单元格边框
from openpyxl.styles import Border,Side
border = Border(
    left=Side(border_style='double', color='FFBB00'),
    right=Side(border_style='double', color='FFBB00'),
    top=Side(border_style='double', color='FFBB00'),
    bottom=Side(border_style='double', color='FFBB00')
)
ws['C1'].border = border

# 单元格对齐方式等
from openpyxl.styles import Alignment
align = Alignment(horizontal='left',vertical='center',wrap_text=True)
ws['D1'].alignment = align

# 第3行行高修改为40
ws.row_dimensions[3].height = 40
# A列列宽修改为30
ws.column_dimensions['A'].width = 30

# 合并一行中的几个单元格
ws.merge_cells('A7:C7')  
# 合并一个矩形区域中的单元格
ws.merge_cells('A9:C13') 
ws['A9'] = '合并单元格'

wb.save('use_openpyxl.xlsx')
